﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Inspinia_MVC5.Data;
using Inspinia_MVC5.Models;
using Webdiyer.WebControls.Mvc;

namespace Inspinia_MVC5.Controllers
{
    
    public class BooksController : Controller
    {
        private BooksSystemContext db = new BooksSystemContext();

        // GET: /Books/
        [Authorize]
        public ActionResult Index(string name, string isbn, int id = 1)
        {
            string cameraPic = Request["photo"];
            var model = db.Books.Include(b => b.BookType).ToList();
            bool admin = true;
            if (Session["role"] != null && Session["role"].ToString() != "Admin")
            {
                admin = false;
            }
            ViewBag.Admin = admin;
            
            if (name != null)
            {
                model = model.Where(x => x.BookName.Contains(name)).ToList();
            }
            if (isbn != null)
            {
                model = model.Where(x => x.BokISBN.Contains(isbn)).ToList();
            }
            model = model.OrderByDescending(a => a.ID).ToPagedList(id,10);

            if (Request.IsAjaxRequest())
                return PartialView("_BooksList", model);
            return View(model);
        }
        [AdminAuthorize]
        public ActionResult Borrow(string name,string isbn,int id= 1)
        {
            var model = db.Books.ToList();
            if (name != null)
            {
                model = model.Where(x => x.BookName.Contains(name)).ToList();
            }
            if (isbn != null)
            {
                model = model.Where(x => x.BokISBN.Contains(isbn)).ToList();
            }
            model = model.OrderByDescending(a => a.ID).ToPagedList(id, 10);

            if (Request.IsAjaxRequest())
                return PartialView("_BookList", model);
            return View(model);
        }

        // GET: /Books/Details/5

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        // GET: /Books/Create
        [AdminAuthorize]
        public ActionResult Create()
        {
            ViewBag.BookTypeId = new SelectList(db.BookTypes, "ID", "TypeName");
            return View();
        }

        // POST: /Books/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AdminAuthorize]
        public ActionResult Create([Bind(Include="ID,BookName,BokISBN,BookAuthor,BookPress,BookPic,BookMoney,BookOverdue,Status,BookNum,BookDesc,BookTypeId,BookJieYueLiang")] Book book)
        {
            if (ModelState.IsValid)
            {
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase f = Request.Files["photo"];
                    if (f.ContentLength > 0)
                    {
                        string finename = @"~/Content/BookImages/" + book.BokISBN + f.FileName.Substring(f.FileName.LastIndexOf('.'));
                        f.SaveAs(Server.MapPath(finename));
                        book.BookPic = finename;
                    }
                    
                }
                book.Status = 1;
                book.BookOverdue =book.BookMoney * 0.01m;
                db.Books.Add(book);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.BookTypeId = new SelectList(db.BookTypes, "ID", "TypeName", book.BookTypeId);
            return View(book);
        }

        // GET: /Books/Edit/5
        [AdminAuthorize]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            ViewBag.BookTypeId = new SelectList(db.BookTypes, "ID", "TypeName", book.BookTypeId);
            return View(book);
        }

        // POST: /Books/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AdminAuthorize]
        public ActionResult Edit([Bind(Include="ID,BookName,BokISBN,BookAuthor,BookPress,BookPic,BookMoney,BookOverdue,Status,BookNum,BookDesc,BookTypeId,BookJieYueLiang")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Entry(book).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BookTypeId = new SelectList(db.BookTypes, "ID", "TypeName", book.BookTypeId);
            return View(book);
        }

        // GET: /Books/Delete/5
        [AdminAuthorize]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Book book = db.Books.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(book);
        }

        // POST: /Books/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [AdminAuthorize]
        public ActionResult DeleteConfirmed(int id)
        {
            Book book = db.Books.Find(id);
            db.Books.Remove(book);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
